package com.ykl.demo.ctrl;

import com.ykl.base.restful.BasicEntityServiceCtrl;
import com.ykl.base.restful.ByIdForm;
import com.ykl.base.restful.Res;
import com.ykl.demo.ctrl.vm.SpcStationWithRoomsVm;
import com.ykl.demo.entity.SpcRoom;
import com.ykl.demo.entity.SpcStation;
import com.ykl.demo.model.StationRoomStats;
import com.ykl.demo.service.SpcRoomService;
import com.ykl.demo.service.SpcStationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/spcstation")
public class SpcStationCtrl extends BasicEntityServiceCtrl<SpcStation> {

    private SpcStationService mmServ;

    @Autowired
    private GenericConversionService mmServConv;

    public SpcStationCtrl(SpcStationService service) {
        super(service);
        this.mmServ = service;
    }



    /*
     * 增加/修改局站接口.
     *//*
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<Res<SpcStation>> save(
            @RequestBody @Valid
                    SpcStation room,
            BindingResult valRes
    ){
        return validate(valRes,"请求格式错误，详情：", ()-> super.save(room)) ;
    }*/

    /**
     * 查询局站接口.
     * <p>
     * 接口协议	HTTP POST<br/>
     * 数据格式	Json<br/>
     * 接口描述	根据局站ID查询对应的局站信息，并携带下属机房列表。<br/>
     * </p>
     * @param form 查询表单.
     * @param valRes 表单验证结果.
     * @return 应答.
     */
    @RequestMapping(value = "/one", method = RequestMethod.POST)
    public ResponseEntity<Res<SpcStationWithRoomsVm>> findById(
            @RequestBody @Valid
                    ByIdForm form,
            BindingResult valRes){
        return validate(valRes,"查询局站接口请求格式错误，详情：",
                ()-> mmServConv.convert(super.findById(form.getId()).orElseThrow(
                        ()->new RuntimeException("无法找到"+form.getId()+"对应的局站.")
                ), SpcStationWithRoomsVm.class));
    }

    /**
     * 统计接口.
     * <p>
     * 接口协议	HTTP POST<br/>
     * 数据格式	Json<br/>
     * 接口描述	根据局站ID统计对应局站内所有“机房等级”内所有“机房类型”的机房数量。<br/>
     * </p>
     * @param form 统计表单.
     * @param valRes 表单检查结果.
     * @return 统计结果.
     */
    @RequestMapping(value = "/stats", method = RequestMethod.POST)
    public ResponseEntity<Res<List<StationRoomStats>>> statsById(
            @RequestBody @Valid
                    ByIdForm form,
            BindingResult valRes){

        return validate(valRes,"统计接口请求格式错误，详情：",
                ()-> mmServ.stats(form.getId()
                ));

    }

}
